/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.epfc.dnet.mastermind.controller;

import eu.epfc.dnet.mastermind.services.StatsManager;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Pierre Buvé & Nicolas Van Damme
 */
@Controller
public class StatsController {

    private StatsManager statsManager;

    /**
     * Injection du service rankingManager
     * @param rankingManager 
     */
    @Autowired
    public void setStatsManager(StatsManager statsManager) {
        this.statsManager = statsManager;
    }

    /**
     * Méthode appeler quand la page stats.htm est appelé
     * 
     * @param request
     * @param statsModel
     * @return 
     */
    @RequestMapping("/stats.htm")
    protected ModelAndView displayStatsPage(HttpServletRequest request, Model statsModel) {
        if (request.getSession().getAttribute("username") != null) {
            String decoButton = "Déconnecter "+ request.getSession().getAttribute("username");
            statsModel.addAttribute("decobutton",decoButton);
        }

        String firstuser;
        String lastuser;
        String nbruser;
        String nbrallgame;
        String nbrvictory;
        String nbrdefeat;
        String nbrnew;
        String nbrplaying;
        String maxtime;
        
        String nbrVertDsSolution;
        String nbrRougeDsSolution;
        String nbrBleuDsSolution;
        String nbrMauveDsSolution;
        
        String nbrOrangeDsSolution;
        String nbrJauneDsSolution;
        String nbrNoirDsSolution;
        String nbrBlancDsSolution;
        
        Float pourcentVertDsSolution;
        Float pourcentRougeDsSolution;
        Float pourcentBleuDsSolution;
        Float pourcentMauveDsSolution;
        
        Float pourcentOrangeDsSolution;
        Float pourcentJauneDsSolution;
        Float pourcentNoirDsSolution;
        Float pourcentBlancDsSolution;
        
        // on récupère les valeurs auprès des services concernés.
        
        firstuser = statsManager.getFirstUser();
        lastuser = statsManager.getLastUser();
        nbruser = statsManager.getNbrUser();
        nbrallgame = statsManager.getNbrAllGame();
        nbrvictory = statsManager.getNbrVictory();
        nbrdefeat = statsManager.getNbrDefeat();
        nbrnew = statsManager.getNbrNew();
        nbrplaying = statsManager.getNbrPlaying();
        maxtime = statsManager.getMaxTime();
        
        nbrVertDsSolution = statsManager.getNbrCouleurDsSolution("vert");
        nbrRougeDsSolution = statsManager.getNbrCouleurDsSolution("rouge");
        nbrBleuDsSolution = statsManager.getNbrCouleurDsSolution("bleu");
        nbrMauveDsSolution = statsManager.getNbrCouleurDsSolution("mauve");
        
        nbrOrangeDsSolution = statsManager.getNbrCouleurDsSolution("orange");
        nbrJauneDsSolution = statsManager.getNbrCouleurDsSolution("jaune");
        nbrNoirDsSolution = statsManager.getNbrCouleurDsSolution("noir");
        nbrBlancDsSolution = statsManager.getNbrCouleurDsSolution("blanc");
        
        pourcentVertDsSolution = (Float.parseFloat(nbrVertDsSolution)/Float.parseFloat(nbrallgame))*100;
        pourcentRougeDsSolution = (Float.parseFloat(nbrRougeDsSolution)/Float.parseFloat(nbrallgame))*100;
        pourcentBleuDsSolution = (Float.parseFloat(nbrBleuDsSolution)/Float.parseFloat(nbrallgame))*100;
        pourcentMauveDsSolution = (Float.parseFloat(nbrMauveDsSolution)/Float.parseFloat(nbrallgame))*100;
        
        pourcentOrangeDsSolution = (Float.parseFloat(nbrOrangeDsSolution)/Float.parseFloat(nbrallgame))*100;
        pourcentJauneDsSolution = (Float.parseFloat(nbrJauneDsSolution)/Float.parseFloat(nbrallgame))*100;
        pourcentNoirDsSolution = (Float.parseFloat(nbrNoirDsSolution)/Float.parseFloat(nbrallgame))*100;
        pourcentBlancDsSolution = (Float.parseFloat(nbrBlancDsSolution)/Float.parseFloat(nbrallgame))*100;
       
        // on ajoute les valeurs au model.
        
        statsModel.addAttribute("nbruser", nbruser);
        statsModel.addAttribute("firstuser", firstuser);
        statsModel.addAttribute("lastuser", lastuser);
        statsModel.addAttribute("nbrallgame", nbrallgame);
        statsModel.addAttribute("nbrvictory", nbrvictory);
        statsModel.addAttribute("nbrdefeat", nbrdefeat);
        statsModel.addAttribute("nbrnew", nbrnew);
        statsModel.addAttribute("nbrplaying", nbrplaying);
        statsModel.addAttribute("maxtime", maxtime);
        
        statsModel.addAttribute("nbrVertDsSolution", nbrVertDsSolution);
        statsModel.addAttribute("nbrRougeDsSolution", nbrRougeDsSolution);
        statsModel.addAttribute("nbrBleuDsSolution", nbrBleuDsSolution);
        statsModel.addAttribute("nbrMauveDsSolution", nbrMauveDsSolution);
        
        statsModel.addAttribute("nbrOrangeDsSolution", nbrOrangeDsSolution);
        statsModel.addAttribute("nbrJauneDsSolution", nbrJauneDsSolution);
        statsModel.addAttribute("nbrNoirDsSolution", nbrNoirDsSolution);
        statsModel.addAttribute("nbrBlancDsSolution", nbrBlancDsSolution);
        
        statsModel.addAttribute("pourcentVertDsSolution", pourcentVertDsSolution.intValue());
        statsModel.addAttribute("pourcentRougeDsSolution", pourcentRougeDsSolution.intValue());
        statsModel.addAttribute("pourcentBleuDsSolution", pourcentBleuDsSolution.intValue());
        statsModel.addAttribute("pourcentMauveDsSolution", pourcentMauveDsSolution.intValue());
        
        statsModel.addAttribute("pourcentOrangeDsSolution", pourcentOrangeDsSolution.intValue());
        statsModel.addAttribute("pourcentJauneDsSolution", pourcentJauneDsSolution.intValue());
        statsModel.addAttribute("pourcentNoirDsSolution", pourcentNoirDsSolution.intValue());
        statsModel.addAttribute("pourcentBlancDsSolution", pourcentBlancDsSolution.intValue());
        
        //on envoie le model.
        return new ModelAndView("stats", "model", statsModel);
    }
}
